package cn.iocoder.yudao.module.english.controller.admin.reward;

import cn.iocoder.yudao.module.english.controller.admin.reward.vo.RewardConfigurationPageReqVO;
import cn.iocoder.yudao.module.english.controller.admin.reward.vo.RewardConfigurationRespVO;
import cn.iocoder.yudao.module.english.controller.admin.reward.vo.RewardConfigurationSaveReqVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.english.dal.dataobject.reward.RewardConfigurationDO;
import cn.iocoder.yudao.module.english.service.reward.RewardConfigurationService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 奖励配置")
@RestController
@RequestMapping("/english/reward-configuration")
@Validated
public class RewardConfigurationController {

    @Resource
    private RewardConfigurationService rewardConfigurationService;

    @PostMapping("/create")
    @Operation(summary = "创建奖励配置")
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:create')")
    public CommonResult<Long> createRewardConfiguration(@Valid @RequestBody RewardConfigurationSaveReqVO createReqVO) {
        return success(rewardConfigurationService.createRewardConfiguration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新奖励配置")
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:update')")
    public CommonResult<Boolean> updateRewardConfiguration(@Valid @RequestBody RewardConfigurationSaveReqVO updateReqVO) {
        rewardConfigurationService.updateRewardConfiguration(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除奖励配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:delete')")
    public CommonResult<Boolean> deleteRewardConfiguration(@RequestParam("id") Long id) {
        rewardConfigurationService.deleteRewardConfiguration(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得奖励配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:query')")
    public CommonResult<RewardConfigurationRespVO> getRewardConfiguration(@RequestParam("id") Long id) {
        RewardConfigurationDO rewardConfiguration = rewardConfigurationService.getRewardConfiguration(id);
        return success(BeanUtils.toBean(rewardConfiguration, RewardConfigurationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得奖励配置分页")
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:query')")
    public CommonResult<PageResult<RewardConfigurationRespVO>> getRewardConfigurationPage(@Valid RewardConfigurationPageReqVO pageReqVO) {
        PageResult<RewardConfigurationDO> pageResult = rewardConfigurationService.getRewardConfigurationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RewardConfigurationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出奖励配置 Excel")
    @PreAuthorize("@ss.hasPermission('english:reward-configuration:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRewardConfigurationExcel(@Valid RewardConfigurationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RewardConfigurationDO> list = rewardConfigurationService.getRewardConfigurationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "奖励配置.xls", "数据", RewardConfigurationRespVO.class,
                        BeanUtils.toBean(list, RewardConfigurationRespVO.class));
    }

}